// index.js
import Toast from '../../miniprogram/miniprogram_npm/@vant/weapp/toast/toast';
// import { decode } from 'base64-arraybuffer';
const fsm = wx.getFileSystemManager();

const request = require('../../util/request')
Page({
    data: {
        currentTab: 0,// 默认选中第一个
        // list3:{
        //   TEXT0:'入库单号1',
        //   TEXT1:'-',
        //   TEXT2:'-',
        //   TEXT3:'-',
        //   TEXT4:'-',
        //   TEXT:'-',
        // },
        islook_chan_qu:false,//查看产区
        islook_bao_gao:false,//查看检测报告
        show_chan_di:false,//展示产地图片
        show_he_ge:false,//展示合格图片
        show_cai_zai:false,//展示采摘图片
        show_yun_shu:false,//展示运输图片
        show_jia_gong:false,//展示加工图片
        show_cang_chu:false,//仓储加工图片
        currentIndex: 0,
        plantingList:[],//种植溯源
        planList:[],//计划列表
        fid:'',//
        chakanList:[],//点击查看生产展示的图片 加载之后立即转换
        jianceList:[],//点击查看检测展示的图片 加载之后立即转换
        machining_traceList:[],//获取加工溯源数据
        groupedImages: [],
        groupCount: 0,
        currentGroupIndex: 0,
        keyword:'',//仓储溯源搜索
        getwarehousing_trace:[],//获取仓储溯源数据
    },

    setTabIndex(e) {
      this.loadTabContent(0)
        this.setData({
            tabIndex: e.currentTarget.dataset.index
        })
    },

    goPage(event) {
        if (event.currentTarget.dataset.pageurl == "/pages/register/register") {
            Toast('暂未开放注册~');
        }
        wx.navigateTo({
            url: event.currentTarget.dataset.pageurl,
        })
    },
    onLoad(options) {
      //默认数据,后续修改
      console.log(options,"rrrrrr")
      this.setData({fid:options.fid})
      this.localNum(0)
      this.getchakanListList()
      // this.getjianceListList()
      this.getmachining_trace()
      this.getwarehousing_trace()
      
    },

    //种植溯源
    getchakanListList(){
      let tmp = { fid: this.data.fid }
      var that = this
      request.requestGet("fruit/plant_trace", tmp, "", function(res){
        console.log(res,"res--种植溯源")
        if (res.status === 200) {
          that.setData({
            plantingList: res.data
          })
        }
        that.groupImages();
      }, function(res) {
        wx.showToast({ title: '错误', icon: 'none' });
      }, false);
    },
    //计划列表
    getjianceListList(){
      let tmp = { fid: this.data.fid }
      var that = this
      
      request.requestGet("fruit/plant_trace", tmp, "", function(res){
        console.log(res,'计划列表')
        if (res.status === 200) {
          that.setData({
            plantingList: res.data
          })
        }
      }, function(res) {
        wx.showToast({ title: '错误', icon: 'none' });
      }, false);
    },
    //加工溯源
    getmachining_trace(){
      var that = this
      let tmp = { fid: that.data.fid }
      request.requestGet("fruit/machining_trace", tmp, "", function(res){
        console.log(res,"ressssssss")
        if (res.status === 200) {
          that.setData({
            machining_traceList: res.data
          })
          console.log(that.data.machining_traceList,"ASSSSSSSSSS")
        }
      }, function(res) {
        wx.showToast({ title: '错误', icon: 'none' });
      }, false);
    },
    //仓储溯源
    getwarehousing_trace(){
      var that = this
      let tmp = { fid: that.data.fid,keyword:that.data.keyword}
      request.requestGet("fruit/warehousing_trace", tmp, "查询中", function(res){
        console.log(res,"getwarehousing_tracegetwarehousing_trace")
        if (res.status === 200) {
          that.setData({
            getwarehousing_trace: res.data
          })
        }
      }, function(res) {
        wx.showToast({ title: '错误', icon: 'none' });
      }, false);

      const data = that.data.getwarehousing_trace; // 您的原始数据
    
      // 为每个项目添加 show_cang_chu 属性
      const processedData = data.map(item => {
        return {
          ...item,
          show_cang_chu: false // 默认不显示图片
        };
      });
      
      this.setData({
        getwarehousing_trace: processedData
      });
      
    },

    onShow() {
    },
    onReady() {

    },

    localNum(e){
      this.setData({
        currentTab: e
      });
    },
    switchTab(e) {
        this.setData({
          currentTab: Number(e.currentTarget.dataset.index),
          islook_chan_qu:false,//查看产区
          islook_bao_gao:false,//查看检测报告
          show_chan_di:false,//展示产地图片
          show_he_ge:false,//展示合格图片
          show_cai_zai:false,//展示采摘图片
          show_yun_shu:false,//展示运输图片
          show_jia_gong:false,//展示加工图片
          // show_cang_chu:false,//仓储加工图片
        });
        // for(let i = 0;i<this.data.getwarehousing_trace.length;i++){
        //     console.log(this.data.getwarehousing_trace[i],"pppppppp")
        // }
        const updatedData = this.data.getwarehousing_trace.map(item => {
          return {
            ...item,
            show_cang_chu: false // 强制设置为false
          };
        });
        
        this.setData({
          getwarehousing_trace: updatedData
        });
    },

    //查看产区
    chanqu(){
      let tmpisBool = this.data.show_chan_di
      this.setData({
        show_chan_di:!tmpisBool
      })
    },
    //查看报告
    baogao(){
      let tmpisBool = this.data.islook_bao_gao
      this.setData({
        islook_bao_gao:!tmpisBool
      })
    },

    //查看采摘日期
    caizai(){
      let tmpisBool = this.data.show_cai_zai
      this.setData({
        show_cai_zai:!tmpisBool
      })
    },
    //查看运输日期
    yunshu(){
      let tmpisBool = this.data.show_yun_shu
      this.setData({
        show_yun_shu:!tmpisBool
      })
    },
    //查看加工日期
    jiagong(){
      let tmpisBool = this.data.show_jia_gong
      this.setData({
        show_jia_gong:!tmpisBool
      })
    },
    
    

    //跳转去工作
    towork(){
      // workLayer
      wx.navigateTo({ url: '/pages/baogong/baogong' })
    },
    get_worker(){
      wx.navigateTo({ url: '/pages/baogongren/baogongren' })
    },
    get_bill_info(e){
      console.log(e,"eeeee")
      let fid = e.currentTarget.dataset.index
      wx.navigateTo({ url: `/pages/get_bill_info/get_bill_info?fid=${fid}` })
    },


    //---------------
    groupImages: function() {
      const picArr = this.data.plantingList.picArr;
      const grouped = [];
      
      console.log('原始图片数组:', picArr);
      console.log('图片数量:', picArr.length);
      
      // 每3张图片为一组
      for (let i = 0; i < picArr.length; i += 3) {
        const group = picArr.slice(i, i + 3);
        grouped.push(group);
        console.log(`第${grouped.length}组:`, group);
      }
      
      console.log('分组结果:', grouped);
      console.log('总组数:', grouped.length);
      
      this.setData({
        groupedImages: grouped,
        groupCount: grouped.length
      }, () => {
        console.log('数据更新完成', this.data.groupedImages);
      });
    },
    
    // 上一组
    prevGroup: function() {
      if (this.data.currentGroupIndex > 0) {
        this.setData({
          currentGroupIndex: this.data.currentGroupIndex - 1
        });
      }
    },
    
    // 下一组
    nextGroup: function() {
      if (this.data.currentGroupIndex < this.data.groupCount - 1) {
        this.setData({
          currentGroupIndex: this.data.currentGroupIndex + 1
        });
      }
    },

    tologin(){
      wx.navigateTo({ url: '/pages/login/login' })
    },
    
    get_report_works(){
      wx.navigateTo({ url: '/pages/get_report_works/get_report_works' })
    },
    get_barcode_list(){
      wx.navigateTo({ url: '/pages/get_barcode_list/get_barcode_list' })
    },
    cangchu(e){
      const index = e.currentTarget.dataset.index;
      const key = `getwarehousing_trace[${index}].show_cang_chu`;
      
      this.setData({
        [key]: !this.data.getwarehousing_trace[index].show_cang_chu
      });
    },


    onSearch(e){
      console.log(e,"eeeeeeee")
      this.setData({keyword:e.detail})
      this.getwarehousing_trace()
    },

    scanCode(e){
      console.log(e,"用户点击扫码")
      wx.scanCode({
        onlyFromCamera: false, // 允许从相册选择
        scanType: ['qrCode'],  // 仅识别二维码
        success: (res) => {
          console.log(res,"成功")
          console.log(res.result,"成功")
          const url = res.result; // 获取二维码内容
          // if(errMsg)
          let tmpinfo = res.result
          let tmplist
          tmplist = tmpinfo.split('?id=')
          console.log(tmplist,"11111111111")
          // if(tmplist[1] === undefined || tmplist[1] === null){
          //   console.log("AAAAAAAAAAAAA")
          //   wx.showToast({
          //     title: '二维码不符合规则',
          //     icon: 'none',
          //     duration: 1000  // 2秒后自动关闭
          //   })
          //   return false
          // }
          console.log(tmplist[1],"tmplist[1]")
          let tmpid = tmplist[1]
          wx.navigateTo({
            url: `/pages/get_barcode_info/get_barcode_info?id=${tmplist}`
          });
        },
        fail: (err) => {
          console.log(err,"失败")
          wx.showToast({ title: '扫码失败', icon: 'none' });
        }
      });
    },
})

